/*
 * Copyright (C) 2009, 2010, B3log Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.b3log.nide.service.maven;

import java.io.File;
import org.b3log.latke.service.ServiceException;
import org.b3log.nide.model.Project;
import org.b3log.nide.service.ProjectManager;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/**
 * {@link MavenManager} test case.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.6, Jul 6, 2010
 */
public final class MavenManagerTestCase {

    /**
     * Maven manager.
     */
    private MavenManager mavenManager;
    /**
     * Test group id.
     */
    private static final String TEST_GROUP_ID = "org.b3log";
    /**
     * Test artifact id.
     */
    public static final String TEST_ARTIFACT_ID = "nide-test";
    /**
     * Test projects directory.
     */
    private static final String TEST_PROJECTS_DIR =
            ProjectManager.getProjectsDir();

    /**
     * Initializes test instance.
     */
    @BeforeTest
    public void beforeTest() {
        mavenManager = MavenManager.INSTANCE;
    }

    /**
     * Deletes test project.
     */
    @AfterTest
    public void afterTest() {
        final File testProject = new File(TEST_PROJECTS_DIR + TEST_ARTIFACT_ID);
        // TODO: delete test project
        testProject.delete();
    }

    /**
     * Tests {@linkplain MavenManager#genSampleProject(org.json.JSONObject)}
     * method.
     *
     * @throws Exception exception
     */
    @Test
    public void genSampleProject() throws Exception {
        final JSONObject projectInitParams = new JSONObject();
        projectInitParams.put(Project.PROJECT_GROUP_ID, TEST_GROUP_ID);
        projectInitParams.put(Project.PROJECT_ARTIFACT_ID, TEST_ARTIFACT_ID);
        projectInitParams.put(Project.PROJECT_PARENT_DIR, TEST_PROJECTS_DIR);

        mavenManager.genSampleProject(projectInitParams);
        // TODO: remove test genSampleProject
    }

    /**
     * Tests {@linkplain MavenManager#installProject(org.json.JSONObject)}
     * method.
     *
     * @throws ServiceException service exception
     * @throws JSONException json exception
     */
    //@Test
    public void installProject() throws ServiceException, JSONException {
        final JSONObject project = new JSONObject();
        project.put(Project.PROJECT_BASE_DIR,
                    TEST_PROJECTS_DIR + TEST_ARTIFACT_ID);

        mavenManager.installProject(project);
    }

    /**
     * Tests {@linkplain MavenManager#cleanProject(org.json.JSONObject)}
     * method.
     *
     * @throws ServiceException service exception
     * @throws JSONException json exception
     */
    @Test
    public void cleanProject() throws ServiceException, JSONException {
        final JSONObject project = new JSONObject();
        project.put(Project.PROJECT_BASE_DIR,
                    TEST_PROJECTS_DIR + TEST_ARTIFACT_ID);

        mavenManager.cleanProject(project);
    }
}
